package code.main.controller.alert
{
	import code.common.assets.AssetsClassName;
	import code.main.modle.lang.Language;
	
	import com.adobe.viewsource.ViewSource;
	import com.yy.manager.ResourceManager;
	import com.yy.manager.UiManager;
	import com.yy.ui.controls.Button;
	import com.yy.yyui.button.SwitchButton;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 *通用弹框控制器 
	 * @author YY
	 * 
	 */
	public class AlertCtrl
	{
		public static const BTN_TYPE_OK:int = 1;//显示确定按钮
		public static const BTN_TYPE_OK_CANCLE:int = 2;//显示确定取消按钮
		public static const RESULT_TYPE_OK:int = 1;//点击确定状态
		public static const RESULT_TYPE_CANCLE:int = 2;//点击取消状态
		private var _cb:Function;//回调函数
		private var _view:MovieClip;
		private var _msg:TextField;
		private var _okBtn:Button;
		private var _cancleBtn:Button;
		private var _closeBtn:SimpleButton;
		
		public function AlertCtrl()
		{
			
		}
//################################系统弹框###########################################
		public function sysMsgBox(msg:Object="msg", cb:Function=null, btnType:int=BTN_TYPE_OK, cancleStr:String="", okStr:String=""):void {
			_cb = cb;
			var x:Number = 0;
			var y:Number = 0;
			_view = ResourceManager.getMovieClipByName(AssetsClassName.SYS_MSG_UI);
			if(okStr == "") okStr = Language.OK;
			if(cancleStr == "") cancleStr = Language.CANCLE;
			_view.okBtn.label.text = okStr;
			_view.cancleBtn.label.text = cancleStr;
			x = _view.okBtn.x;
			_view.okBtn.x = 0;
			_okBtn = new Button(_view.okBtn);
			_okBtn.x = x;
			_okBtn.mouseChildren = false;
			_view.addChild(_okBtn);
			x = _view.cancleBtn.x;
			_view.cancleBtn.x = 0;
			_cancleBtn = new Button(_view.cancleBtn);
			_cancleBtn.x = x;
			_cancleBtn.mouseChildren = false;
			_view.addChild(_cancleBtn);
			_closeBtn = _view.closeBtn;
			_msg = _view.text;
			setContent(msg);
			setBtn(btnType);
			addEvent();
			UiManager.getPopUpManager().addPopUp(_view, true);
		}
		
		private function addEvent():void {
			_okBtn.addEventListener(MouseEvent.CLICK, clickFunc);
			_cancleBtn.addEventListener(MouseEvent.CLICK, clickFunc);
			_closeBtn.addEventListener(MouseEvent.CLICK, clickCloseFunc);
		}
		private function removeEvent():void {
			_okBtn.removeEventListener(MouseEvent.CLICK, clickFunc);
			_cancleBtn.removeEventListener(MouseEvent.CLICK, clickFunc);
			_closeBtn.removeEventListener(MouseEvent.CLICK, clickCloseFunc);
		}
		
		private function clickCloseFunc(e:Event):void {
			_cb = null;
			close();
		}
		
		private function clickFunc(e:Event):void {
			var obj:Object = e.currentTarget;
			var resultType:int;
			switch(obj) {
				case _okBtn:
					resultType = RESULT_TYPE_OK;
					break;
				case _cancleBtn:
					resultType = RESULT_TYPE_CANCLE;
					break;
			}
			close();
			if(Boolean(_cb)) {
				_cb(resultType);
				_cb = null;
			}
		}
		
		/**
		 *设置内容 
		 * @param msg
		 * 
		 */
		private function setContent(msg:Object):void {
			_msg.mouseEnabled = false;
			if(msg is String) {
				_msg.text = String(msg);
			}else {
				_msg.text = "";
			}
		}
		/**
		 *设置按钮 
		 * @param cancleStr
		 * @param okStri
		 * 
		 */
		private function setBtn(btnType:int):void {
			if(btnType == BTN_TYPE_OK) {
				_cancleBtn.visible = false;
				_okBtn.x = (_view.width - _okBtn.width)/2;
			}
		}
		
		private function close():void {
			removeEvent();
			UiManager.getPopUpManager().removePopUp(_view);
			_view = null;
			_msg = null;
			_okBtn = null;
			_cancleBtn = null;
			_closeBtn = null;
		}
	}
}